//
//  WelcomeView.swift
//  LEOweibo
//
//  Created by zz on 16/3/6.
//  Copyright © 2016年 zz. All rights reserved.
//

import UIKit

class WelcomeView: UIView {
    
    //MARK:- 构造函数
    override init(frame: CGRect) {
        super.init(frame: UIScreen.mainScreen().bounds)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:- 视图生命周期
    ///将要显示的时候
    override func didMoveToWindow() {
        super.didMoveToWindow()
        
        avatarView.snp_updateConstraints { (make) -> Void in
            make.centerY.equalTo(self).offset(-100)
        }

//        UIView.animateWithDuration(1.0, animations: { () -> Void in
//            self.layoutIfNeeded()
//            }) { (_) -> Void in
//               self.removeFromSuperview()
//        }
        UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.layoutIfNeeded()
            }) { (_) -> Void in
                self.removeFromSuperview()
        }
    }
    
    //MARK:- 私有控件
    private  lazy var avatarView: UIImageView = UIImageView(leoImage: "avatar_default_big")
    private  lazy var welcomeLabel: UILabel = UILabel(leo_text: "欢迎归来", fontSize: 14, color: UIColor.darkGrayColor())
    
}


//MARK:- 设置界面
extension WelcomeView {
    ///界面设置
    private func setupUI() {
        
        backgroundColor = UIColor(patternImage: UIImage(named: "ad_background")!)
        addSubview(avatarView)
        addSubview(welcomeLabel)
        
        let iconWH: CGFloat = 45
        avatarView.layer.cornerRadius = iconWH
        avatarView.layer.masksToBounds = true
        
        avatarView.snp_makeConstraints(closure: { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(100)
            make.size.equalTo(CGSize(width: 2*iconWH, height: 2*iconWH))
        })
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(avatarView)
            make.centerY.equalTo(avatarView.snp_bottom).offset(20)
        }
        //强行更新约束
        layoutIfNeeded()
        //设置头像
        avatarView.leoSetImage(UserAccount.sharedUserAccount.avatar_large, placeholder: "avatar_default_big")
    }
    
}
